<template>
	<view id="popup">
		<view class="popup-con">
			<!-- <view class="close-box" v-if="!ads_type">
				<text class="time">{{time}}s</text>
				<u-icon name="close" size="26" color="#fff" @click="closePopup"></u-icon>
			</view> -->
			<view class="ad">
				<view v-if="!ads_type">
					<image class="image" @click="handlePopup" :src="ads_img" mode="aspectFit">
						<cover-view class="pop-close">
							<cover-view class="top-close">{{time}}s</cover-view>
							<cover-image @click="closePopup" class="close-icon"
								src="../../static/my_icon/close-bold.png"></cover-image>
						</cover-view>
					</image>
				</view>
				<view v-else class="video-con">
					<video id="myVideo" @play="play" :src="ads_video" @error="videoErrorCallback" page-gesture="true"
						autoplay="true" object-fit="fill" controls="false" show-fullscreen-btn="false"
						show-play-btn="false" :direction="0" style="width: 100%; height: 100vh;">
						<cover-view class="pop-close">
							<cover-view class="top-close">{{time}}s</cover-view>
							<cover-image @click="closePopup" class="close-icon"
								src="../../static/my_icon/close-bold.png"></cover-image>
						</cover-view>
					</video>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAds
	} from "@/api/home.js"
	export default {
		data() {
			return {
				time: 30,
				close_btn: false,
				ads:{},
				ads_img: 'https://scpic.chinaz.net/files/pic/pic9/202009/apic27858.jpg',
				ads_video: 'https://cos-1311433599.cos.ap-nanjing.myqcloud.com/images/2022/07/31/407c17ada3be4f3fb79bbfe05606d369.mp4',
				ads_type: 0, //0-图片，1-视频
			}
		},
		onShow() {
			this.countdown();
			this.fetchAds()
		},
		methods: {
			countdown() {
				let num = 30;
				let timer = setInterval(() => {
					this.time = num--;
					if (this.time == 0) {
						this.closePopup()
						clearInterval(timer);
					}
				}, 1000)
			},
			fetchAds() {
				getAds().then(res => {
					// console.log(res);
					this.ads = res
					this.ads_type = this.checkFile(res.img);
					if (this.ads_type) {
						this.ads_video = res.img
					} else {
						this.ads_img = res.img;
					}
				})
			},
			handlePopup() {
				// console.log(this.ads.url);
				let url = this.ads.url
				if (url.indexOf("https://") != -1 || url.indexOf("http://") != 1) {
					url = "https://" + url
				} else {
					url = url
				}
				// #ifdef H5
				window.location.href = url;
				// #endif
				// #ifdef APP-PLUS
				plus.runtime.openURL(url);
				// #endif
			},
			checkFile(fileValue) {
				if (!fileValue) return
				// console.log("fileValue==" + fileValue);
				var index = fileValue.lastIndexOf("."); //（考虑严谨用lastIndexOf(".")得到）得到"."在第几位
				let fileValueSuffix = fileValue.substring(index); //截断"."之前的，得到后缀
				if (/(.*)\.(mp4|rmvb|avi|ts)$/.test(fileValueSuffix)) { //根据后缀，判断是否符合视频格式
					return 1;
				}
				if (/(.*)\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(fileValueSuffix)) { //根据后缀，判断是否符合图片格式
					return 0;
				}
			},
			play() {
				this.close_btn = true;
				// 获取 video 上下文 videoContext 对象
				this.videoContext = uni.createVideoContext('myVideo');
				// 进入全屏状态
				// this.videoContext.requestFullScreen();
			},
			videoErrorCallback(e) {
				uni.showModal({
					content: e.target.errMsg,
					showCancel: false
				})
			},
			closePopup() {
				uni.reLaunch({
					url: `/pages/home/home?isPopup=true`
				})
			},

		}
	}
</script>

<style lang="scss">
	page {
		background: rgba(0, 0, 0, 0.6);
	}

	/* 关闭 */
	.pop-close {
		position: fixed;
		top: 30px;
		right: 20px;
		width: 80px;
		height: 28px;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 99999;
		color: #fff;
		border-radius: 100px;

		.top-close {
			line-height: 28px;
			text-align: left;
			padding-left: 8px;
		}

		.close-icon {
			position: absolute;
			top: 0;
			right: 10px;
			width: 26px;
			height: 26px;
		}
	}

	.popup-con {
		width: 100%;
		height: 100vh;

		.close {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 5px;
		}

		.close-box {
			position: fixed;
			top: 30px;
			right: 20px;
			width: 80px;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 5px;
			border-radius: 100px;
			padding: 0 10px;
			color: #fff;
			border: 1px solid #fff;

			.time {
				margin-right: 5px;
			}
		}

		.ad {
			text-align: center;
			width: 100%;
			
			.image{
				width: 100%;
				height: 100vh;
			}
			.video-con {
				// margin-top: 150px;
				position: relative;
				z-index: 9999;
				outline: 1px solid red;
			}

			#myVideo {
				width: 750rpx;
				// height: 100vh;
				display: flex;
				justify-content: center;
			}
		}
	}
</style>
